package com.example.examapp.service.impl;

import com.example.examapp.enums.Role;
import com.example.examapp.model.User;
import com.example.examapp.service.UserService;
import org.lzx.base.service.impl.BaseServiceImpl;
import org.lzx.base.utils.ExcelUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @version v1.0
 * @package com.example.examapp.service.impl
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Override
    public List<User> importFormExcel(MultipartFile file) throws IOException {
        List<User> users = ExcelUtils.<List<User>>readExcel(file);
        return (List<User>) super.mongoTemplate.insert(users,User.class);
    }

    @Override
    public User login(String userName, String pwd) {
        User user = super.mongoTemplate.findOne(
                new Query(Criteria.where("userName").is(userName).and("pwd").is(pwd).and("role").is(Role.ADMIN)), User.class);
        return user;
    }
}
